﻿#include "parametermodel.h"

ParameterModel::ParameterModel(QObject *parent)
    : QAbstractListModel(parent)
{

}

ParameterModel::~ParameterModel()
{

}

int ParameterModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return _list.count();
}

QVariant ParameterModel::data(const QModelIndex &index, int role) const
{
    if(index.row() < 0 || index.row() > _list.count())
        return QVariant();

    ParameterItem* item = _list[index.row()];

    switch(role)
    {
    case Qt::UserRole+1:
        return item->id();
        break;
    case Qt::UserRole+2:
        return item->Name();
        break;
    case Qt::UserRole+3:
        return item->Number();
        break;
    case Qt::UserRole+4:
        return item->Knife();
        break;
    case Qt::UserRole+5:
        return item->Roller();
        break;
    case Qt::UserRole+6:
        return item->Stress01();
        break;
    case Qt::UserRole+7:
        return item->Stress02();
        break;
    case Qt::UserRole+8:
        return item->Stress03();
        break;
    case Qt::UserRole+9:
        return item->F_Knife01();
        break;
    case Qt::UserRole+10:
        return item->F_Knife02();
        break;
    case Qt::UserRole+11:
        return item->F_Knife03();
        break;
    case Qt::UserRole+12:
        return item->B_Knife01();
        break;
    case Qt::UserRole+13:
        return item->B_Knife02();
        break;
    case Qt::UserRole+14:
        return item->B_Knife03();
        break;
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> ParameterModel::roleNames() const
{
    QHash<int,QByteArray> roles;
    roles.insert(Qt::UserRole+1,"ID");
    roles.insert(Qt::UserRole+2,"Name");
    roles.insert(Qt::UserRole+3,"Number");
    roles.insert(Qt::UserRole+4,"Knife");
    roles.insert(Qt::UserRole+5,"Roller");
    roles.insert(Qt::UserRole+6,"Stress01");
    roles.insert(Qt::UserRole+7,"Stress02");
    roles.insert(Qt::UserRole+8,"Stress03");
    roles.insert(Qt::UserRole+9,"F_Knife01");
    roles.insert(Qt::UserRole+10,"F_Knife02");
    roles.insert(Qt::UserRole+11,"F_Knife03");
    roles.insert(Qt::UserRole+12,"B_Knife01");
    roles.insert(Qt::UserRole+13,"B_Knife02");
    roles.insert(Qt::UserRole+14,"B_Knife03");

    return roles;
}

void ParameterModel::insertItem(int index, ParameterItem *item)
{
    beginInsertRows(QModelIndex(),index,index);
    _list.insert(index,item);
    endInsertRows();
}

void ParameterModel::addItem(ParameterItem *item)
{
    insertItem(_list.count(),item);
}

void ParameterModel::removeByItem(ParameterItem *item)
{
    int index = _list.indexOf(item);
    removeByIndex(index);
}

void ParameterModel::removeByIndex(int index)
{
    beginRemoveRows(QModelIndex(),index,index);
    _list.removeAt(index);
    endRemoveRows();
}

int ParameterModel::count()
{
    return _list.count();
}

void ParameterModel::copyModel(ParameterModel *model)
{
    beginResetModel();

    qDeleteAll(_list);

    _list.clear();

    for(int i = 0 ; i < model->count(); ++i)
    {
        ParameterItem* item = model->_list.at(i);
        ParameterItem* other = new ParameterItem();
        other->copyItem(item);
        addItem(other);
    }

    endResetModel();
}

ParameterItem *ParameterModel::itemAt(int index)
{
    if(index < 0 || index > _list.count())
    {
        return NULL;
    }

    return _list.at(index);
}

void ParameterModel::clear()
{
    beginResetModel();
    qDeleteAll(_list);
    _list.clear();
    endResetModel();
}

QString ParameterModel::getCurrentValue(int index)
{
    ParameterItem* m_item = itemAt(index);
    QString value = m_item->Name();
    return value;
}

void ParameterModel::freshData()
{
    beginResetModel();
    endResetModel();
}
